package com.mybatis.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;

import com.mybatis.domain.User;
import com.mybatis.mapper.provider.UserMapperProvider;

/**
 * @Description 用户 Dao 层
 *
 * @author 堆堆堆
 * @date 2018年4月20日 下午12:05:01
 */
public interface UserMapper {

	/**
	 * @Description 查询所有用户
	 *
	 * @return
	 */
	@Select("select * from t_user")
	List<User> selectAll();

	/**
	 * @Description 根据用户名模糊查询用户
	 *
	 * @return
	 */
	@Select("select * from t_user where user_name like concat('%', #{userName}, '%')")
	List<User> selectByUserNameLike(String userName);

	/**
	 * @Description 插入用户
	 *
	 * @param user
	 */
	// 插入后自动填入 id 到实体
	//@Options(useGeneratedKeys = true, keyProperty = "id")
	@Insert("insert into t_user(user_name,short_name,age) values(#{userName}, #{shortName}, #{age})")
	Integer insert(User user);

	/**
	 * @Description 根据 id 修改用户信息
	 *
	 * @param user
	 */
	@Update("update t_user set user_name=#{userName}, short_name=#{shortName} where id =#{id}")
	Integer update(User user);

	/**
	 * @Description 根据 id 删除用户
	 *
	 * @param id
	 */
	@Delete("delete from t_user where id =#{id}")
	Integer delete(Integer id);

	/**
	 * @Description 动态条件查询，script 标签版
	 *
	 * @return
	 */
	@Select({ "<script>",
		"select * from t_user ",
		"where 1=1 ",
		"<when test='userName != null'>",
		"and user_name like concat('%', #{userName}, '%') ",
		"</when>",
	"</script>" })
	// 这里不加 @Param 就报错咯...
	List<User> selectUser(@Param("userName") String userName);

	/**
	 * @Description 动态条件查询，provider 实现 sql 拼接版
	 *
	 * @return
	 */
	@SelectProvider(type = UserMapperProvider.class, method = "selectUser2")
	List<User> selectUser2(String userName);
}
